package com.sctvcloud.zhaohua_sd.ui.adapter.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.TextViewExpandableAnimation;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.FComment;
import com.sctvcloud.zhaohua_sd.ui.util.IListShowData;
import com.sctvcloud.zhaohua_sd.ui.utils.GlideCircleTransform;
import com.sctvcloud.zhaohua_sd.ui.utils.GlideUtil;
import com.sctvcloud.zhaohua_sd.utils.UserManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/24 0024.
 */

public class TopicDetailCommentHolder extends BaseAbsHolder<FComment> {

    @BindView(R.id.comment_layout)
    protected RelativeLayout commentLayout;
    @BindView(R.id.item_topic_detail_comment_icon)
    protected CustomEXImageView img;
    @BindView(R.id.item_topic_detail_comment_username)
    protected CustomFontTextView username;
    @BindView(R.id.item_topic_detail_comment_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_topic_detail_comment_comment)
    protected TextViewExpandableAnimation content;
    @BindView(R.id.item_topic_detail_comment_reply)
    protected CustomFontTextView reply;
    @BindView(R.id.item_topic_detail_comment_divider)
    protected View divider;

    @BindView(R.id.answer_layout)
    protected RelativeLayout answerLayout;
    @BindView(R.id.answer_item_topic_detail_comment_icon)
    protected CustomEXImageView answer_img;
    @BindView(R.id.answer_item_topic_detail_comment_username)
    protected CustomFontTextView answer_username;
    @BindView(R.id.answer_item_topic_detail_comment_time)
    protected CustomFontTextView answer_time;
    @BindView(R.id.answer_item_topic_detail_comment_comment)
    protected TextViewExpandableAnimation answer_content;
    @BindView(R.id.answer_item_topic_detail_comment_reply)
    protected View answer_reply;
    @BindView(R.id.answer_item_topic_detail_comment_divider)
    protected View answerDivider;

    private FComment comment;

    private List<IListShowData> list;
    private boolean lastCommentIsReply = false;

    public TopicDetailCommentHolder(final Context context, List<IListShowData> list, View itemView) {
        super(context, itemView);
        this.list = list;
        content.setOnStateChangeSetting(new TextViewExpandableAnimation.OnStateChangeSetting() {
            @Override
            public void reInitStateView(TextView stateView) {
                stateView.setText(R.string.newes_see_all);
            }

            @Override
            public void onStateChanged(TextView stateView, boolean isShrink) {
                JLog.e(" is shrink=" + isShrink + "   " + this);
                content.getStateImg().setRotation(isShrink ? 90 : -90);
            }
        });

        content.setOnStateChangeListener(new TextViewExpandableAnimation.OnStateChangeListener() {
            @Override
            public void onStateChange(boolean isShrink) {
                if (comment != null) {
                    comment.setShrink(isShrink);
                }
            }
        });
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(FComment fComment) {
        this.comment = fComment;
        int position = getAdapterPosition();
        if (null != list && list.size() > position && position > 0) {
//            if (!TextUtils.isEmpty(list.get(position - 1).getAnswerTo())) {
//                //上一条为回复评论
//                lastCommentIsReply = true;
//            } else {
//                lastCommentIsReply = false;
//            }
        }
        if (TextUtils.isEmpty(comment.getAnswerTo())) {
            if (position == 0) {
                divider.setVisibility(View.GONE);
            } else {
                divider.setVisibility(View.VISIBLE);
            }
            commentLayout.setVisibility(View.VISIBLE);
            answerLayout.setVisibility(View.GONE);
            reply.setVisibility(View.GONE);
            username.setText(comment.getNickName());
            reply.setVisibility(View.VISIBLE);


            time.setText(DateUtils.formatDate(DateUtils.getDataDate(comment), ""));
            content.setText(comment.getComment());
            content.resetState(comment.isShrink());
            if (!TextUtils.isEmpty(comment.getAvatar())) {
                GlideUtil.getGlideWithAvatorDef(context, comment.getAvatar()).error(R.mipmap.icon_circle_avatar)
                        .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context)).into(img);
            }
        } else {
            commentLayout.setVisibility(View.GONE);
            answerLayout.setVisibility(View.VISIBLE);
            answer_username.setText(comment.getNameSpanned());
            answer_reply.setVisibility(View.GONE);
            if (lastCommentIsReply) {
                answerDivider.setVisibility(View.VISIBLE);
            } else {
                answerDivider.setVisibility(View.GONE);
            }
            answer_time.setText(DateUtils.formatDate(DateUtils.getDataDate(comment), ""));
            answer_content.setText(comment.getComment());
            answer_content.resetState(comment.isShrink());
            if (!TextUtils.isEmpty(comment.getAvatar())) {
                GlideUtil.getGlideWithAvatorDef(context, comment.getAvatar()).error(R.mipmap.icon_circle_avatar)
                        .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context)).into(answer_img);
            }
        }
//        if (TextUtils.isEmpty(fComment.getAnswerTo())) {
//            username.setText(fComment.getNickName());
//            reply.setVisibility(View.VISIBLE);
//        } else {
//            username.setText(fComment.getNameSpanned());
//            reply.setVisibility(View.GONE);
//        }
//        time.setText(DateUtils.timeAgo(DateUtils.getDataDate(fComment)));
//        content.setText(fComment.getComment());
//        if (!TextUtils.isEmpty(fComment.getAvatar())) {
//            GlideUtil.getGlideWithAvatorDef(context, fComment.getAvatar()).error(R.mipmap.icon_circle_avatar)
//                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context)).into(img);
//        }
//        content.resetState(fComment.isShrink());
    }

    @OnClick(value = {R.id.item_topic_detail_comment_reply})
    public void reply(View view) {
        if (internalClick != null) {
            itemView.setTag(R.id.adapter_item_data_key, comment);
            internalClick.onItemInternalClick(itemView, view, getAdapterPosition());
        }
    }

    @Override
    public TopicDetailCommentHolder setInternalClick(OnItemInternalClick internalClick) {
        super.setInternalClick(internalClick);
        return this;
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
        if (mode == BaseAbsHolder.ITEMMODE_NORMAL) {
            divider.setVisibility(View.VISIBLE);
        } else {
            divider.setVisibility(View.GONE);
        }
    }
}
